package com.kmzx.gen.signs.events;

import com.kmzx.gen.signs.config.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;


@Component
public class JRedisMessageListener implements MessageListener {
    public final static String LISTENER_PATTERN = "__key*__:*";

    private static Logger logger = LoggerFactory.getLogger(JRedisMessageListener.class);


    @Autowired
    ApplicationContext applicationContext;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        byte[] body = message.getBody();// 建议使用: valueSerializer

        byte[] channel = message.getChannel();


        logger.info("channel: {}, body: {}, bytes: {}", new String(channel), new String(body), new String(pattern));

        String combineOrder = new String(body);

        String prefix = combineOrder.substring(0, 2);

        logger.info("prefix = " + prefix);
        logger.info("combineOrder = " + combineOrder);


        if (prefix != null && Constants.sea_upload_Prefix.equals(prefix)) {
            String endInfo = combineOrder.substring(2);
            String[] s = endInfo.split("_");
            BeSignEvent signEvent = new BeSignEvent(s[0]);
            applicationContext.publishEvent(signEvent);
        }



    }
}
